﻿using LitJson;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

public class ControleTrigger:ShipEventTrigger
{
    public ShipUI shipUI { get { return m_ShipUI; } set { m_ShipUI = value; m_BindID = m_ShipUI.id; } }
    public int operationType { get { return m_OperationType; } set { m_OperationType = value; } }
    int m_BindID=-1;
    int m_OperationType;

    bool m_IsTrigger = false;
    ShipUI m_ShipUI;
    public ControleTrigger()
    {
        m_TriggerType = TriggerType.ControleSystem;
    }

    public override void BindShip(Ship ship)
    {
        if (ship == null)
        {
            Debug.LogWarning("ShipEventTrigger::BindShip(Ship) Cann't found ship in ShipEventTriggerr");
            return;
        }
        if (m_BindID < 0)
            return;
        m_ShipUI = ship.FindUI(m_BindID);
    }
    //  只有舰船被创建的时候被调用一次
    public override void Init()
    {
        if (m_ShipUI == null)
            return;
        BindUI(m_ShipUI);
    }
    public override bool CheckTrigger()
    {
        var trigger = m_IsTrigger;
        m_IsTrigger = false;
        return trigger;
    }

    public override string GetName()
    {
        return "无";
    }
    public virtual string GetOprationTypeName(int index)
    {
        return "无";
    }
    public virtual List<string> GetOprationList()
    {
        List<string> oprationList = new List<string>();
        return oprationList;
    }
    public virtual void BindUI(ShipUI shipUI)
    {
        if(m_OperationType >= 0)
            shipUI.BindOperationEvent(m_OperationType, OnEventTrigger);
        return;
    }
    public void OnEventTrigger()
    {
        m_IsTrigger = true;
    }
    public override JsonData Save()
    {
        JsonData root = base.Save();

        root["BindID"] = m_BindID;
        root["OperationType"] = m_OperationType;
        return root;
    }
    public override bool Load(JsonData root)
    {
        if (!base.Load(root))
            return false;
        IDictionary<string, JsonData> rootObj = root.ValueAsObject();

        if (rootObj.ContainsKey("BindID"))
            m_BindID = rootObj["BindID"].ValueAsInt();

        if (rootObj.ContainsKey("OperationType"))
            m_OperationType = rootObj["OperationType"].ValueAsInt();
        return true;
    }
}